package org.nightrunner.d3.controller.management;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.nightrunner.d3.constant.CRUDConstant;
import org.nightrunner.d3.entity.Property;
import org.nightrunner.d3.log.SysLog;
import org.nightrunner.d3.model.DefaultPaginationInfo;
import org.nightrunner.d3.model.IPaginationInfo;
import org.nightrunner.d3.model.PaginationModel;
import org.nightrunner.d3.service.IPropertyService;
import org.nightrunner.d3.service.ISearchService;
import org.nightrunner.d3.service.impl.JsonService;
import org.nightrunner.d3.util.OperateInfo;
import org.nightrunner.d3.util.OperateInfo.Link;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.JsonObject;

/**
 * Matter Management Controller
 * 
 * @author haiyang.chen
 * @date 2012-5-15
 * @version 1.0
 */
@Controller
@RequestMapping("/management/equipment/property/")
public class PropertyManagementController implements CRUDConstant {

	private static final String RETURN_VIEW_PREFIX = "/management/equipment/property/";

	private static final String LINK_INDEX = "/management/equipment/property/index";

	private static final String LINK_NAME_INDEX = "属性管理首页";

	public static final Logger LOGGER = SysLog.CONTROLLER;

	@Autowired
	private IPropertyService propertyService;

	public IPropertyService getPropertyService() {
		return propertyService;
	}

	public void setPropertyService(IPropertyService propertyService) {
		this.propertyService = propertyService;
	}

	@RequestMapping(value = "list-{currentPage}-{rowSize}", method = RequestMethod.GET)
	public String list(HttpServletResponse response,
			HttpServletRequest request, ModelMap modelMap,
			@PathVariable("rowSize") Integer rowSize,
			@PathVariable("currentPage") Integer currentPage) {
		IPaginationInfo pagination = new DefaultPaginationInfo();
		pagination.setCurrentPage(currentPage);
		pagination.setRowSize(rowSize);

		PaginationModel<List<Property>> paginationModel = propertyService
				.getPropertys(pagination);
		modelMap.addAttribute("paginationModel", paginationModel);
		return RETURN_VIEW_PREFIX + VIEW_LIST;
	}

	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String index(HttpServletResponse response,
			HttpServletRequest request, ModelMap modelMap) {
		return list(response, request, modelMap, 10, 0);
	}

	@RequestMapping(value = "", method = RequestMethod.GET)
	public String root(HttpServletResponse response,
			HttpServletRequest request, ModelMap modelMap) {
		return list(response, request, modelMap, 10, 0);
	}

	@RequestMapping(value = "preupdate/{propertyId}", method = RequestMethod.GET)
	public String preUpdate(HttpServletResponse response,
			HttpServletRequest request, ModelMap modelMap,
			@PathVariable("propertyId") String propertyId) {
		Property property = propertyService.getProperty(propertyId);
		modelMap.addAttribute("property", property);
		return RETURN_VIEW_PREFIX + VIEW_UPDATE;
	}

	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String add(HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap) {

		Property property = new Property();
		String propertyName = request.getParameter("propertyName");
		String propertyAbbreviationName = request
				.getParameter("propertyAbbreviationName");
		String propertyKeywords = request.getParameter("propertyKeywords");
		String propertyDescription = request
				.getParameter("propertyDescription");

		property.setPropertyName(propertyName);
		property.setPropertyAbbreviationName(propertyAbbreviationName);
		property.setPropertyDescription(propertyDescription);
		property.setPropertyKeywords(propertyKeywords);

		propertyService.updateProperty(property);

		OperateInfo operateInfo = new OperateInfo();
		operateInfo.setInfo(OPERATE_INFO_UPDATE_OK);
		operateInfo.addLink(new Link(LINK_NAME_INDEX, LINK_INDEX));
		modelMap.addAttribute(REQUEST_KEY_OPERATE_INFO, operateInfo);

		return VIEW_OPERATE_INFO;
	}

	@RequestMapping(value = "preadd", method = RequestMethod.GET)
	public String preAdd(HttpServletResponse response,
			HttpServletRequest request, ModelMap modelMap) {

		// Property property = propertyService.getProperty(propertyId);
		//
		// String propertyName = request.getParameter("propertyName");
		// String propertyKeywords = request.getParameter("propertyKeywords");
		// String propertyItemLevel =
		// request.getParameter("propertyItemLevel");
		// String propertyDescription = request
		// .getParameter("propertyDescription");
		//
		// property.setPropertyName(propertyName);
		// property.setPropertyDescription(propertyDescription);
		// property.setPropertyItemLevel(propertyItemLevel);
		// property.setPropertyKeywords(propertyKeywords);
		//
		// propertyService.updateProperty(property);
		//
		// OperateInfo operateInfo = new OperateInfo();
		// operateInfo.setInfo(OPERATE_INFO_UPDATE_OK);
		// operateInfo.addLink(new Link(LINK_NAME_INDEX, LINK_INDEX));
		// modelMap.addAttribute(REQUEST_KEY_OPERATE_INFO, operateInfo);

		return RETURN_VIEW_PREFIX + VIEW_ADD;
	}

	@RequestMapping(value = "update/{propertyId}", method = RequestMethod.POST)
	public String update(HttpServletResponse response,
			HttpServletRequest request, ModelMap modelMap,
			@PathVariable("propertyId") String propertyId) {

		Property property = propertyService.getProperty(propertyId);

		String propertyName = request.getParameter("propertyName");
		String propertyAbbreviationName = request
				.getParameter("propertyAbbreviationName");
		String propertyKeywords = request.getParameter("propertyKeywords");
		String propertyDescription = request
				.getParameter("propertyDescription");

		property.setPropertyName(propertyName);
		property.setPropertyAbbreviationName(propertyAbbreviationName);
		property.setPropertyDescription(propertyDescription);
		property.setPropertyKeywords(propertyKeywords);

		propertyService.updateProperty(property);

		OperateInfo operateInfo = new OperateInfo();
		operateInfo.setInfo(OPERATE_INFO_UPDATE_OK);
		operateInfo.addLink(new Link(LINK_NAME_INDEX, LINK_INDEX));
		modelMap.addAttribute(REQUEST_KEY_OPERATE_INFO, operateInfo);

		return VIEW_OPERATE_INFO;
	}

	@Autowired
	private ISearchService searchService;

	public ISearchService getSearchService() {
		return searchService;
	}

	public void setSearchService(ISearchService searchService) {
		this.searchService = searchService;
	}

	// @RequestMapping(value = "search/{keyword}-{currentPage}-{rowSize}",
	// method = RequestMethod.GET)
	// public String result(ModelMap modelMap,
	// @PathVariable("keyword") String keyword,
	// @PathVariable("currentPage") Integer currentPage,
	// @PathVariable("rowSize") Integer rowSize) throws Exception {
	// if (keyword == null || keyword.trim().length() == 0) {
	// return "redirect:/" + RETURN_VIEW_PREFIX + VIEW_INDEX;
	// }
	//
	// IPaginationInfo pagination = new DefaultPaginationInfo();
	// pagination.setCurrentPage(currentPage);
	// pagination.setRowSize(rowSize);
	//
	// List<String> keywords = new ArrayList<String>();
	// keywords.add(keyword);
	//
	// PaginationModel<List<Property>> paginationModel = searchService
	// .searchPropertys(keywords, pagination);
	// modelMap.addAttribute("keyword", keyword);
	//
	// modelMap.addAttribute("paginationModel", paginationModel);
	//
	// return RETURN_VIEW_PREFIX + "search/" + VIEW_LIST;
	// }

	@RequestMapping(value = "delete/{propertyId}", method = {
			RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public String delete(@PathVariable("propertyId") String propertyId,
			HttpServletResponse response, ModelMap modelMap) {
		JsonObject object = new JsonObject();
		Property property = propertyService.getProperty(propertyId);

		if (property == null) {
			object.addProperty(CRUD_SUCCESS, false);
			object.addProperty(CRUD_INFO, "事项不存在");
		} else {
			try {
				propertyService.deleteProperty(property);
				object.addProperty(CRUD_SUCCESS, true);
			} catch (Exception ex) {
				object.addProperty(CRUD_SUCCESS, false);
			}
		}

		return object.toString();
	}

	@RequestMapping(value = "ajaxgetpropertys", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
	public String ajaxgetpropertys(HttpServletResponse response,
			ModelMap modelMap) {
		JsonObject object = new JsonObject();
		List<Property> propertys = propertyService.getAllProperty();
		try {
			object.addProperty(CRUD_SUCCESS, true);

			object.add("objects", jsonService.getObjects(propertys));
			return object.toString();
		} catch (Exception ex) {
			object.addProperty(CRUD_SUCCESS, false);
			return object.toString();
		}
	}

	/**
	 * 
	 */
	@Autowired
	private JsonService jsonService;

	public JsonService getJsonService() {
		return jsonService;
	}

	public void setJsonService(JsonService jsonService) {
		this.jsonService = jsonService;
	}

}
